const { list } = require('./src/mock')
const bodyParser = require('body-parser')
module.exports = {
	devServer: {
		before(app) {
			app.use(bodyParser.json())
			// 接口 
			// 列表 (支持搜索)
			// 可以传递 searchValue
			app.get('/api/lists', (req, res) => {
				const { searchValue } = req.query

				if(searchValue) {
					const result = list.filter(v => v.title.includes(searchValue) || v.desc.includes(searchValue));

					res.send({
						code: 200,
						data: result
					})
				} else {
					res.send({
						code: 200,
						data: list
					})
				}
			
			})

		

			// 详情接口 动态路由
			app.get('/api/post/:id', (req, res) => {
				const { id } = req.params
				const item = list.find(v => v.id === id)
				res.send({
					code: 200,
					data: item
				})
			})

			// 详情接口
			app.get('/api/post/', (req, res) => {
				const { id } = req.query
				const item = list.find(v => v.id === id)
				res.send({
					code: 200,
					data: item
				})
			})

			// 详情接口
			app.post('/api/post/', (req, res) => {
				const { id } = req.body
				const item = list.find(v => v.id === id)
				res.send({
					code: 200,
					data: item
				})
			})
		}
	}
}